package com.sip.stream.utils.zfy.yuv;

import android.graphics.ImageFormat;
import android.media.Image;

import com.sip.stream.utils.ImageYUVBean;

import java.nio.ByteBuffer;

public class ZFYRowPixelStrideYUVTool extends ICamera2ImageReader {
    private static final String TAG = "ZFYRowPixelStrideYUVTool";

    public ZFYRowPixelStrideYUVTool(int width, int height) {
        super(width, height);
    }

    @Override
    public ImageYUVBean getDataFormImage(Image image) {
        Image.Plane[] planes;
        byte[] obtainOutputData = obtainOutputData(this.dataLength);
        int rowStride = image.getPlanes()[0].getRowStride();
        for (Image.Plane plane : image.getPlanes()) {
            if (rowStride < plane.getRowStride()) {
                rowStride = plane.getRowStride();
            }
        }
        int bitsPerPixel = ImageFormat.getBitsPerPixel(35) / 8;
        int i = 0;
        for (int i2 = 0; i2 < image.getPlanes().length; i2++) {
            Image.Plane plane2 = image.getPlanes()[i2];
            ByteBuffer buffer = plane2.getBuffer();
            plane2.getPixelStride();
            int rowStride2 = plane2.getRowStride();
            if (i2 == 0) {
                int i3 = this.height;
                int i4 = this.width * bitsPerPixel;
                for (int i5 = 0; i5 < i3; i5++) {
                    buffer.get(obtainOutputData, i, i4);
                    i += i4;
                    if (i5 < i3 - 1) {
                        buffer.position((buffer.position() + rowStride2) - i4);
                    }
                }
            } else if (i2 != 1) {
                int i6 = this.uvHeight;
                int i7 = this.width * bitsPerPixel;
                for (int i8 = 0; i8 < i6; i8++) {
                    int i9 = i6 - 1;
                    if (i8 == i9) {
                        i7--;
                    }
                    buffer.get(obtainOutputData, i, i7);
                    i += i7;
                    if (i8 < i9) {
                        buffer.position((buffer.position() + rowStride2) - i7);
                    }
                }
            }
            buffer.rewind();
        }
        ImageYUVBean imageYUVBean = new ImageYUVBean();
        imageYUVBean.setCameraData(obtainOutputData);
        imageYUVBean.setImageTimestampus(image.getTimestamp() / 1000);
        return imageYUVBean;
    }
}
